import React, { Component } from 'react';
import EncryptUtil from '../util/EncryptUtil';
import {
    View,
    Text,
    Image,
    TextInput,
    StyleSheet
} from 'react-native';
import BaseComponentF from './BaseComponentF';
import Constants from '../constant/Constants';
import LoginButton from '../component/LoginButton';
import AHHttpUtil from '../util/AHHttpUtil';
import StorageUtil from '../util/StorageUtil';
/**
 * 登录页面
 */
class LoginPage extends Component {
    constructor(props) {
        super(props);
        this.props.navigation.setParams({
            title: '修改标题'
        });
        this.state = {
            username: '15611110012',
            pwd: ''
        };
    }
    render() {
        return (
            <View style={Style.container}>
                <Image style={Style.logoImg} source={require('../img/logo_launch.png')} />
                <TextInput style={Style.input}
                    placeholder='账号'
                    clearButtonMode='while-editing'
                    defaultValue={this.state.username}
                    underlineColorAndroid='transparent'
                    onChangeText={(username) => { this.setState({ username }) }} />
                <TextInput style={Style.input}
                    placeholder='密码'
                    clearButtonMode='while-editing'
                    password={true}
                    underlineColorAndroid='transparent'
                    onChangeText={(pwd) => { this.setState({ pwd: EncryptUtil.md5(pwd) }) }} />
                <LoginButton text='登录' onPress={() => { this.login() }} />
            </View>
        );

    }

    /**
     * 登录方法
     */
    login() {
        AHHttpUtil.request('POST', Constants.URL.URL_LOGIN, this.state)
            .then((response) => {
                console.log(response);
                if (response.returncode == 0) {
                    //登录成功
                    StorageUtil.save(Constants.KEY.KEY_LOGIN_INFO, response.result)
                    .then(()=>{
                        alert(response.message);
                        this.props.navigation.goBack();
                    })
                    .catch((error)=>{
                        alert(`保存登录信息失败`);
                    });
                    
                } else {
                    //登录失败
                    alert(response.message);
                }
            }, (error) => {
                alert(error.message);
            })
            .catch(error => {
                alert(error);
            });

    }
}

const Style = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        backgroundColor: 'white',
    },
    logoImg: {
        width: 150,
        height: 80,
        resizeMode: Image.resizeMode.contain
    },
    input: {
        backgroundColor: '#f0f0f0',
        paddingLeft: 10,
        paddingRight: 10,
        width: 240,
        height: 40,
        marginTop: 20,
    },


});


export default BaseComponentF(LoginPage, '我的登录页');